<?php
/**
 * 自定义标签云小工具
 */
class Custom_Tag_Cloud_Widget extends WP_Widget {

    // 构造函数
    public function __construct() {
        parent::__construct(
            'custom_tag_cloud',
            __('自定义标签云', 'text_domain'),
            array(
                'description' => __('显示自定义样式的标签云', 'text_domain'),
            )
        );
    }

    // 前端显示
    public function widget($args, $instance) {
        $title = apply_filters('widget_title', $instance['title']);
        $number = !empty($instance['number']) ? absint($instance['number']) : 45;
        $orderby = !empty($instance['orderby']) ? $instance['orderby'] : 'name';
        $show_count = isset($instance['show_count']) ? (bool) $instance['show_count'] : false;

        echo $args['before_widget'];

        if (!empty($title)) {
            echo $args['before_title'] . $title . $args['after_title'];
        }

        $tag_cloud = wp_tag_cloud(array(
            'taxonomy'   => 'post_tag',
            'number'     => $number,
            'orderby'    => $orderby,
            'order'      => 'ASC',
            'show_count' => $show_count,
            'echo'       => false
        ));

        if ($tag_cloud) {
            echo '<div class="tagcloud">' . $tag_cloud . '</div>';
        } else {
            echo '<p>' . __('未找到标签', 'text_domain') . '</p>';
        }

        echo $args['after_widget'];
    }

    // 后端表单
    public function form($instance) {
        $defaults = array(
            'title'      => __('热门标签', 'text_domain'),
            'number'     => 45,
            'orderby'    => 'name',
            'show_count' => false
        );
        $instance = wp_parse_args((array) $instance, $defaults);
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>">
                <?php _e('标题：', 'text_domain'); ?>
            </label>
            <input class="widefat" 
                   id="<?php echo $this->get_field_id('title'); ?>" 
                   name="<?php echo $this->get_field_name('title'); ?>" 
                   type="text" 
                   value="<?php echo esc_attr($instance['title']); ?>" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('number'); ?>">
                <?php _e('显示数量：', 'text_domain'); ?>
            </label>
            <input class="tiny-text" 
                   id="<?php echo $this->get_field_id('number'); ?>" 
                   name="<?php echo $this->get_field_name('number'); ?>" 
                   type="number" 
                   value="<?php echo esc_attr($instance['number']); ?>" 
                   step="1" min="1" max="100" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('orderby'); ?>">
                <?php _e('排序方式：', 'text_domain'); ?>
            </label>
            <select class="widefat" 
                    id="<?php echo $this->get_field_id('orderby'); ?>" 
                    name="<?php echo $this->get_field_name('orderby'); ?>">
                <option value="name" <?php selected($instance['orderby'], 'name'); ?>>
                    <?php _e('名称', 'text_domain'); ?>
                </option>
                <option value="count" <?php selected($instance['orderby'], 'count'); ?>>
                    <?php _e('文章数量', 'text_domain'); ?>
                </option>
            </select>
        </p>

        <p>
            <input type="checkbox" 
                   class="checkbox" 
                   id="<?php echo $this->get_field_id('show_count'); ?>" 
                   name="<?php echo $this->get_field_name('show_count'); ?>" 
                   <?php checked($instance['show_count']); ?> />
            <label for="<?php echo $this->get_field_id('show_count'); ?>">
                <?php _e('显示文章数量', 'text_domain'); ?>
            </label>
        </p>
        <?php
    }

    // 更新设置
    public function update($new_instance, $old_instance) {
        $instance = array();
        $instance['title'] = sanitize_text_field($new_instance['title']);
        $instance['number'] = absint($new_instance['number']);
        $instance['orderby'] = sanitize_text_field($new_instance['orderby']);
        $instance['show_count'] = isset($new_instance['show_count']) ? (bool) $new_instance['show_count'] : false;
        return $instance;
    }
}

// 注册小工具
function register_custom_tag_cloud_widget() {
    register_widget('Custom_Tag_Cloud_Widget');
}
add_action('widgets_init', 'register_custom_tag_cloud_widget');